<?php

namespace app\admin\controller\recruit;

use app\common\controller\Backend;
use think\helper\Time;

/**
 * 会员规则管理
 *
 * @icon fa fa-circle-o
 */
class Apply extends Backend
{

    /**
     * Ad模型对象
     */
    protected $model = null;
    protected $adlist = [];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Apply');
    }

    /**
     * 查看
     */
    public function index()
    {

        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model->where($where)->order($sort, $order)->count();
            $list = $this->model->where($where)->order($sort, $order)->limit($offset, $limit)->select();
            foreach($list as &$ad){
                $ad['recruit_title']=model('Recruit')->where('recruit_id', $ad['recruit_id'])->value('recruit_title');
                $ad['recruit_post']=model('Post')->where('post_id', $ad['recruit_post_id'])->value('post_name');
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
       $recruit_list=model('Recruit')->order('recruit_id desc')->select();
       $this->assign('recruit_list',$recruit_list);
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
//        $this->view->assign('row',array());
//        $this->view->assign('loclist', build_select('row[ad_location]', \app\admin\model\AdLocation::column('location_id,location_name'), '', ['class' => 'form-control selectpicker']));
       
        
        
        return parent::add();
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        if ($this->request->isPost()) {//提交表单
            return parent::edit($ids);
        }
        $row = $this->model->get($ids);
        $this->view->assign('apply_row',$row);
        if (!$row) $this->error(__('No Results were found'));
        
        $rec_id=$row['recruit_id'];
        $rec_row=db('hs_recruit')->where('recruit_id',$rec_id)->find();//招聘信息
        $post_list=db('hs_recruit_post')->where('recruit_id',$rec_id)->select();//招聘信息
        $this->assign('post_list',$post_list);
        $this->assign('rec_row',$rec_row);
        //字段查询
        $column_list = db('hs_apply_columns')->where('column_id','in',$rec_row['columns_id'])->order('sort')->select();
        foreach ($column_list as &$column){
            if (in_array($column['column_type'],array('text','select','checkbox','radio'))){
                $column['column_options']=explode(',',$column['column_options']);
            }
        }
        $this->assign('column_list',$column_list);
        
        
        
        return $this->view->fetch();
    }
    /**
     * 准考信息
     */
    public function exam($ids = NULL)
    {
        $row = $this->model->get($ids);
        $this->assign('row',$row);
        if ($this->request->isPost()) {//提交表单
            return parent::edit($ids);
        }
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $delIds = array_unique(explode(',', $ids));
            $count = $this->model->where('apply_id', 'in', $delIds)->delete();
            if ($count)
            {
                $this->success();
            }
        }
        $this->error();
    }
    /**
     * 导入笔试信息
     */
    public function import()
    {
        $file = $this->request->request('file');
        if (!$file) {
            $this->error(__('Parameter %s can not be empty', 'file'));
        }
        $filePath = ROOT_PATH . DS . 'public' . DS . $file;
        if (!is_file($filePath)) {
            $this->error(__('No results were found'));
        }
        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if (!$PHPReader->canRead($filePath)) {
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if (!$PHPReader->canRead($filePath)) {
                $PHPReader = new \PHPExcel_Reader_CSV();
                if (!$PHPReader->canRead($filePath)) {
                    $this->error(__('Unknown data format'));
                }
            }
        }
    
        //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
        $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
    
        $table = $this->model->getQuery()->getTable();
        $database = \think\Config::get('database.database');
        $fieldArr = [];
        $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
        foreach ($list as $k => $v) {
            if ($importHeadType == 'comment') {
                $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
            } else {
                $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
            }
        }
    
        $PHPExcel = $PHPReader->load($filePath); //加载文件
        $currentSheet = $PHPExcel->getSheet(0);  //读取文件中的第一个工作表
        $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
        $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
        $maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn);

        $update = [];
        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
            $data['apply_id']= $currentSheet->getCellByColumnAndRow(0, $currentRow)->getValue();//id
            $data['exam_num'] = $currentSheet->getCellByColumnAndRow(3, $currentRow)->getValue();//笔试考号
            $data['exam_room'] = $currentSheet->getCellByColumnAndRow(4, $currentRow)->getValue();//笔试考场
            $data['exam_seat_num'] = $currentSheet->getCellByColumnAndRow(5, $currentRow)->getValue();//笔试座号
            $data['updatetime'] = time();
            if(!empty($data['apply_id'])){
                $update[]=$data;
            }
        }
        if (!$update) {
            $this->error(__('No rows were updated'));
        }
        try {
            foreach ($update as &$apply){
                model('Apply')->where('apply_id',$apply['apply_id'])->update($apply);
            }
        } catch (\think\exception\PDOException $exception) {
            $this->error($exception->getMessage());
        }
    
        $this->success();
    }
    /**
     * 导入面试信息
     */
    public function import2()
    {
        $file = $this->request->request('file');
        if (!$file) {
            $this->error(__('Parameter %s can not be empty', 'file'));
        }
        $filePath = ROOT_PATH . DS . 'public' . DS . $file;
        if (!is_file($filePath)) {
            $this->error(__('No results were found'));
        }
        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if (!$PHPReader->canRead($filePath)) {
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if (!$PHPReader->canRead($filePath)) {
                $PHPReader = new \PHPExcel_Reader_CSV();
                if (!$PHPReader->canRead($filePath)) {
                    $this->error(__('Unknown data format'));
                }
            }
        }
    
        //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
        $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
    
        $table = $this->model->getQuery()->getTable();
        $database = \think\Config::get('database.database');
        $fieldArr = [];
        $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
        foreach ($list as $k => $v) {
            if ($importHeadType == 'comment') {
                $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
            } else {
                $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
            }
        }
    
        $PHPExcel = $PHPReader->load($filePath); //加载文件
        $currentSheet = $PHPExcel->getSheet(0);  //读取文件中的第一个工作表
        $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
        $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
        $maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn);

        $update = [];
        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
            $data['apply_id']= $currentSheet->getCellByColumnAndRow(0, $currentRow)->getValue();//id
            $data['interview_num'] = $currentSheet->getCellByColumnAndRow(3, $currentRow)->getValue();//面试考号
            $data['interview_room'] = $currentSheet->getCellByColumnAndRow(4, $currentRow)->getValue();//面试考场
            $data['interview_seat_num'] = $currentSheet->getCellByColumnAndRow(5, $currentRow)->getValue();//面试座号
            $data['updatetime'] = time();
            if(!empty($data['apply_id'])){
                $update[]=$data;
            }
        }
        if (!$update) {
            $this->error(__('No rows were updated'));
        }
        try {
            foreach ($update as &$apply){
                model('Apply')->where('apply_id',$apply['apply_id'])->update($apply);
            }
        } catch (\think\exception\PDOException $exception) {
            $this->error($exception->getMessage());
        }
    
        $this->success();
    }

}
